package cn.iocoder.yudao.module.crm.dal.mysql.receipt;

import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.crm.controller.admin.contract.vo.contract.CrmContractPageReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractConfigDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.receipt.ReceiptDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneTypeEnum;
import cn.iocoder.yudao.module.crm.util.CrmPermissionUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.crm.controller.admin.receipt.vo.*;

/**
 * crm发票管理 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface ReceiptMapper extends BaseMapperX<ReceiptDO> {

    /*default PageResult<ReceiptDO> selectPage(ReceiptPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ReceiptDO>()
                .eqIfPresent(ReceiptDO::getCode, reqVO.getCode())
                .eqIfPresent(ReceiptDO::getCustomerId, reqVO.getCustomerId())
                .likeIfPresent(ReceiptDO::getCustomerName, reqVO.getCustomerName())
                .eqIfPresent(ReceiptDO::getContractId, reqVO.getContractId())
                .eqIfPresent(ReceiptDO::getContractName, reqVO.getContractName())
                .eqIfPresent(ReceiptDO::getReceiptAmount, reqVO.getReceiptAmount())
                .eqIfPresent(ReceiptDO::getReceiptDate, reqVO.getReceiptDate())
                .eqIfPresent(ReceiptDO::getReceiptType, reqVO.getReceiptType())
                .eqIfPresent(ReceiptDO::getTitleId, reqVO.getTitleId())
                .eqIfPresent(ReceiptDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ReceiptDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(ReceiptDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(ReceiptDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(ReceiptDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ReceiptDO::getOwnerUserId, reqVO.getOwnerUserId())
                .orderByDesc(ReceiptDO::getId));
    }*/

    default PageResult<ReceiptDO> selectPage(ReceiptPageReqVO reqVO, Long userId) {
        MPJLambdaWrapperX<ReceiptDO> query = new MPJLambdaWrapperX<>();
        // 拼接数据权限的查询条件
        CrmPermissionUtils.appendPermissionCondition(query, CrmBizTypeEnum.CRM_RECEIPT.getType(),
                ReceiptDO::getId, userId, reqVO.getSceneType());
        // 拼接自身的查询条件
        query.selectAll(ReceiptDO.class)
                .eqIfPresent(ReceiptDO::getCode, reqVO.getCode())
                .eqIfPresent(ReceiptDO::getCustomerId, reqVO.getCustomerId())
                .likeIfPresent(ReceiptDO::getCustomerName, reqVO.getCustomerName())
                .eqIfPresent(ReceiptDO::getContractId, reqVO.getContractId())
                .eqIfPresent(ReceiptDO::getContractName, reqVO.getContractName())
                .eqIfPresent(ReceiptDO::getReceiptAmount, reqVO.getReceiptAmount())
                .eqIfPresent(ReceiptDO::getReceiptDate, reqVO.getReceiptDate())
                .eqIfPresent(ReceiptDO::getReceiptType, reqVO.getReceiptType())
                .eqIfPresent(ReceiptDO::getTitleId, reqVO.getTitleId())
                .eqIfPresent(ReceiptDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ReceiptDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(ReceiptDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(ReceiptDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(ReceiptDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ReceiptDO::getOwnerUserId, reqVO.getOwnerUserId())
                .orderByDesc(ReceiptDO::getId);

        return selectJoinPage(reqVO, ReceiptDO.class, query);
    }

    default Long selectCountByAudit(Long userId) {
        MPJLambdaWrapperX<ReceiptDO> query = new MPJLambdaWrapperX<>();
        // 我负责的 + 非公海
        CrmPermissionUtils.appendPermissionCondition(query, CrmBizTypeEnum.CRM_RECEIPT.getType(),
                ReceiptDO::getId, userId, CrmSceneTypeEnum.OWNER.getType());
        // 未审核
        query.eq(ReceiptDO::getAuditStatus, CrmAuditStatusEnum.PROCESS.getStatus());
        return selectCount(query);
    }

}